package com.cyf.library.ext.lifecycle

import android.app.Activity
import java.util.*

/**
 * 对象声明（Object Declaration）
伴生对象（Companion Object）
对象表达式（Object Expression）
 */
object KtxActivityManager {

    private val mActivityList = LinkedList<Activity>()

    val currentActivity: Activity?
        get() = if (mActivityList.isEmpty()) null
        else mActivityList.last

    fun pushActivity(activity: Activity) {
        if (mActivityList.contains(activity)) {
            if (mActivityList.last != activity) {
                mActivityList.remove(activity)
                mActivityList.add(activity)
            }
        } else {
            mActivityList.add(activity)
        }
    }


    fun popActivity(activity: Activity) {
        mActivityList.remove(activity)
    }

    fun finishCurrentActivity() {
        currentActivity?.finish()
    }

    fun finishActivity(activity: Activity) {
        mActivityList.remove(activity)
        activity.finish()
    }

}